package cn.ztuo.bitrade.service;

import cn.ztuo.bitrade.dao.BourseProductManagerDao;
import cn.ztuo.bitrade.entity.BourseProductManager;
import cn.ztuo.bitrade.pagination.Criteria;
import cn.ztuo.bitrade.pagination.Restrictions;
import cn.ztuo.bitrade.service.Base.BaseService;
import cn.ztuo.bitrade.util.BeanUtils;
import cn.ztuo.bitrade.vo.product.ProductManagerAddReqVo;
import cn.ztuo.bitrade.vo.product.ProductManagerFindReqVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class BourseProductManagerService extends BaseService {
    @Autowired
    private BourseProductManagerDao mapper;

    @Transactional(rollbackFor = Exception.class)
    public BourseProductManager saveAndFlush(ProductManagerAddReqVo reqVo) {
        BourseProductManager productManager = new BourseProductManager();
        BeanUtils.copyProperties(reqVo, productManager);
        productManager.setCreateTime(new Date());
        productManager.setUpdateTime(new Date());
        return mapper.saveAndFlush(productManager);
    }
    @Transactional(rollbackFor = Exception.class)
    public BourseProductManager saveAndFlush(BourseProductManager productManager) {
        return mapper.saveAndFlush(productManager);
    }

    public void delete(BourseProductManager BourseProductManager) {
        mapper.delete(BourseProductManager);
    }

    public BourseProductManager get(Long id) {
        return mapper.getById(id);
    }
    public BourseProductManager getByProductCode(String productCode) {
        return mapper.getByProductCode(productCode);
    }

    public Page<BourseProductManager> findAllByPage(ProductManagerFindReqVo reqVo) {
        Sort orders = new Sort(new Sort.Order(Sort.Direction.DESC, "createTime"));
        if(reqVo.getPageNo()==null||reqVo.getPageNo()==0){
            reqVo.setPageNo(1);
        }
        if(reqVo.getPageSize()==null||reqVo.getPageSize()==0){
            reqVo.setPageSize(10);
        }
        PageRequest pageRequest = new PageRequest(reqVo.getPageNo() - 1, reqVo.getPageSize(), orders);
        Criteria<BourseProductManager> specification = new Criteria<BourseProductManager>();
        specification.add(Restrictions.eq("productCode", reqVo.getProductCode(), true));
        specification.add(Restrictions.like("productName", reqVo.getProductName(), true));
        specification.add(Restrictions.eq("status", reqVo.getStatus(), true));

        return mapper.findAll(specification, pageRequest);
    }

    public List<BourseProductManager> findAll(ProductManagerFindReqVo reqVo) {
        Criteria<BourseProductManager> specification = new Criteria<BourseProductManager>();
        specification.add(Restrictions.eq("productCode", reqVo.getProductCode(), true));
        specification.add(Restrictions.like("productName", reqVo.getProductName(), true));
        specification.add(Restrictions.eq("status", reqVo.getStatus(), true));
        specification.sort("createTime.desc");
        return mapper.findAll(specification);
    }

    //----------------------------------------------排行查询---------------------------------------------------

    public List<Map<String,Object>> exchangeHandRate(JSONObject reqVo) {
        List<Object[]> list = mapper.exchangeHandRate(reqVo.getString("symbol"),reqVo.getLong("start"),reqVo.getLong("end"),reqVo.getString("name"),reqVo.getLong("id"));
        List<Map<String,Object>> listMap = new ArrayList<>();
        for(int i =0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i)[0]);
            map.put("name",list.get(i)[1]);
            map.put("count",list.get(i)[2]);
            map.put("symbol",list.get(i)[3]);
            listMap.add(map);
        }
        return listMap;
    }
    public List<Map<String,Object>> contractHandRate(JSONObject reqVo) {
        List<Object[]> list =  mapper.contractHandRate(reqVo.getString("symbol"),reqVo.getLong("start"),reqVo.getLong("end"),reqVo.getString("name"),reqVo.getLong("id"));
        List<Map<String,Object>> listMap = new ArrayList<>();
        for(int i =0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i)[0]);
            map.put("name",list.get(i)[1]);
            map.put("count",list.get(i)[2]);
            map.put("symbol",list.get(i)[3]);
            listMap.add(map);
        }
        return listMap;
    }
    public List<Map<String,Object>> exchangeAmount(JSONObject reqVo) {
        List<Object[]> list =  mapper.exchangeAmount(reqVo.getString("symbol"),reqVo.getLong("start"),reqVo.getLong("end"),reqVo.getString("name"),reqVo.getLong("id"));
        List<Map<String,Object>> listMap = new ArrayList<>();
        for(int i =0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i)[0]);
            map.put("name",list.get(i)[1]);
            map.put("count",list.get(i)[2]);
            map.put("symbol",list.get(i)[3]);
            listMap.add(map);
        }
        return listMap;
    }
    public List<Map<String,Object>> contractAmount(JSONObject reqVo) {
        List<Object[]> list =  mapper.contractAmount(reqVo.getString("symbol"),reqVo.getLong("start"),reqVo.getLong("end"),reqVo.getString("name"),reqVo.getLong("id"));
        List<Map<String,Object>> listMap = new ArrayList<>();
        for(int i =0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i)[0]);
            map.put("name",list.get(i)[1]);
            map.put("count",list.get(i)[2]);
            map.put("symbol",list.get(i)[3]);
            listMap.add(map);
        }
        return listMap;
    }
    public List<Map<String,Object>> amountRank(JSONObject reqVo) {
        List<Object[]> list =  mapper.amountRank(reqVo.getString("symbol"));
        List<Map<String,Object>> listMap = new ArrayList<>();
        for(int i =0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i)[0]);
            map.put("name",list.get(i)[1]);
            map.put("count",list.get(i)[2]);
            map.put("symbol",list.get(i)[3]);
            listMap.add(map);
        }
        return listMap;
    }
    public List<Map<String,Object>> chargeRank(JSONObject reqVo) {
        List<Object[]> list =  mapper.chargeRank(reqVo.getString("symbol"),reqVo.getDate("start"),reqVo.getDate("end"));
        List<Map<String,Object>> listMap = new ArrayList<>();
        for(int i =0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i)[0]);
            map.put("name",list.get(i)[1]);
            map.put("count",list.get(i)[2]);
            map.put("symbol",list.get(i)[3]);
            listMap.add(map);
        }
        return listMap;
    }
    public List<Map<String,Object>> extractRank(JSONObject reqVo) {
        List<Object[]> list =  mapper.extractRank(reqVo.getString("symbol"),reqVo.getDate("start"),reqVo.getDate("end"));
        List<Map<String,Object>> listMap = new ArrayList<>();
        for(int i =0;i<list.size();i++){
            Map<String,Object> map = new HashMap<>();
            map.put("id",list.get(i)[0]);
            map.put("name",list.get(i)[1]);
            map.put("count",list.get(i)[2]);
            map.put("symbol",list.get(i)[3]);
            listMap.add(map);
        }
        return listMap;
    }


}
